package com.example.administrator.about_huanxin.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.about_huanxin.FragmentFactory;
import com.example.administrator.about_huanxin.R;
import com.example.administrator.about_huanxin.activity.SearchActivity;
import com.example.administrator.about_huanxin.fragment.tongxun.HaoYouFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/8.
 */
public class TongxunFragment extends BaseFragment {


    @Bind(R.id.haoyou_tab_line)
    View haoyouTabLine;
    @Bind(R.id.haoyou_tab)
    LinearLayout haoyouTab;
    @Bind(R.id.guanzhu_tab_line)
    View guanzhuTabLine;
    @Bind(R.id.guanzhu_tab)
    LinearLayout guanzhuTab;
    @Bind(R.id.fensi_tab_line)
    View fensiTabLine;
    @Bind(R.id.fensi_tab)
    LinearLayout fensiTab;
    @Bind(R.id.qunzu_tab_line)
    View qunzuTabLine;
    @Bind(R.id.qunzu_tab)
    LinearLayout qunzuTab;
    @Bind(R.id.renzheng_tab_line)
    View renzhengTabLine;
    @Bind(R.id.renzheng_tab)
    LinearLayout renzhengTab;
    @Bind(R.id.tongxun_container)
    FrameLayout tongxunContainer;


    private FragmentManager fm;
    private FragmentTransaction ft;
    private HaoYouFragment haoYouFragment;



    @Override
    public void setEmptyView(TextView tv, boolean isVisible) {
        tv.setText("可以让附近的人发现，空的");
        tv.setVisibility(View.GONE);
    }

    @Override
    public void setTitleText(TextView tv) {
        tv.setText("好友");
    }

    @Override
    public void setTitleButton(Button btn) {
        btn.setText("添加好友");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
    }

    @Override
    public View setContainer(LayoutInflater inflater, FrameLayout fram) {
        View view = inflater.inflate(R.layout.fragment_tongxunlu, fram, true);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initContent(View content) {
        setTab(0);
    }

    private void setTab(int i) {
        fm = getFragmentManager();
        ft = fm.beginTransaction();
        clearLine();
        switchLine(i);
        hideFragment();
        try {
            if (haoYouFragment == null) {
                haoYouFragment = new HaoYouFragment();
                ft.add(R.id.tongxun_container, haoYouFragment);
            } else {
                ft.show(haoYouFragment);
            }

            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void switchLine(int i) {
        switch (i) {
            case 0:
                haoyouTabLine.setVisibility(View.VISIBLE);
                break;

            case 1:
                guanzhuTabLine.setVisibility(View.VISIBLE);

                break;

            case 2:
                fensiTabLine.setVisibility(View.VISIBLE);
                break;

            case 3:
                qunzuTabLine.setVisibility(View.VISIBLE);
                break;

            case 4:
                renzhengTabLine.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void clearLine() {
        haoyouTabLine.setVisibility(View.INVISIBLE);
        guanzhuTabLine.setVisibility(View.INVISIBLE);
        fensiTabLine.setVisibility(View.INVISIBLE);
        qunzuTabLine.setVisibility(View.INVISIBLE);
        renzhengTabLine.setVisibility(View.INVISIBLE);
    }

    private void hideFragment() {
        if (haoYouFragment != null) {
            ft.hide(haoYouFragment);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.haoyou_tab, R.id.guanzhu_tab, R.id.fensi_tab, R.id.qunzu_tab, R.id.renzheng_tab})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.haoyou_tab:
                setTab(0);
                break;
            case R.id.guanzhu_tab:
                setTab(1);
                break;
            case R.id.fensi_tab:
                setTab(2);
                break;
            case R.id.qunzu_tab:
                setTab(3);
                break;
            case R.id.renzheng_tab:
                setTab(4);
                break;
        }
    }
}
